package com.iot.core.utils;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Collectors;

public class ListUtils {
    /**
     * list分段
     *
     * @param list     要分段的list
     * @param pageSize 每段大小
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        List<List<T>> listArray = new ArrayList<List<T>>();

        ArrayList<T> al = new ArrayList<T>();
        for (T x : list) {
            al.add(x);
            if (pageSize == al.size()) {
                listArray.add(al);
                al = new ArrayList<T>();
            }
        }
        if (0 != al.size()) {
            listArray.add(al);
        }

        return listArray;
    }

    // 将List<String>转化为Sring
    public static String listToString(List<String> strs) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < strs.size(); i++) {
            if (i == (strs.size() - 1)) {
                builder.append("'").append(strs.get(i)).append("'");
            } else {
                builder.append("'").append(strs.get(i)).append("',");
            }
        }
        return builder.toString();
    }

    /**
     * 将List<String>转化为String，并用ex分隔
     *
     * @param strs
     * @param ex
     * @return
     */
    public static String listToString(List<String> strs, String ex) {
        StringBuffer bf = new StringBuffer();
        for (int i = 0; i < strs.size(); i++) {
            if (i != strs.size() - 1) {
                bf.append(strs.get(i)).append(ex);
            } else {
                bf.append(strs.get(i));
            }
        }
        return bf.toString();
    }

    public static String listToSaveString(List<String> strs) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < strs.size(); i++) {
            if (i == (strs.size() - 1)) {
                builder.append("[").append(strs.get(i)).append("]");
            } else {
                builder.append("[").append(strs.get(i)).append("],");
            }
        }
        return builder.toString();
    }

    // String[]转换成String
    public static String arrayToString(String[] strArr) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < strArr.length; i++) {
            if (i == (strArr.length - 1)) {
                builder.append("'").append(strArr[i]).append("'");
            } else {
                builder.append("'").append(strArr[i]).append("',");
            }
        }
        return builder.toString();
    }

    // list分段
    public static <T> List<List<T>> segmentationList(List<T> list, int pageSize) {
        List<List<T>> listList = null;
        if (null != list && list.size() > 0) {
            // 判断是否要分批处理
            listList = new ArrayList<List<T>>();
            if (list.size() > pageSize) {
                listList = ListUtils.splitList(list, pageSize);
            } else {
                listList.add(list);
            }
        }
        return listList;
    }

    /**
     * 判断list是否为空
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> boolean isEmpty(List<T> list) {
        if (null != list && list.size() > 0) {
            return false;
        }
        return true;
    }

    // 模糊查找key
    public static List<String> getLikeByMap(Map<String, String> map, String keyLike) {
        List<String> list = new Vector<>();
        for (Map.Entry<String, String> entity : map.entrySet()) {
            if (entity.getKey().indexOf(keyLike) > -1) {
                list.add(entity.getValue());
            }

        }
        return list;
    }

    /**
     * 改变指定位置集合的值
     *
     * @param list
     * @param index
     * @param value
     * @return
     */
    public static List<String> changeValue(List<String> list, Integer index, String value) {
        List<String> res = Lists.newArrayList();
        for (int i = 0; i < list.size(); i++) {
            if (i == index) {
                res.add(value);
            } else {
                res.add(list.get(i));
            }
        }
        return res;
    }

    /**
     * 划分删除和新增的数据
     *
     * @param newList
     * @param oldList
     * @return
     */
    public static Map<String, List<String>> divideDelAndAddDate(List<String> newList, List<String> oldList) {
        Map<String, List<String>> result = new HashMap<>();
        //newList相比于oldList新增的数据
        result.put("addList", null);
        //newList相比于oldList删除的数据
        result.put("removeList", null);
        if (isEmpty(newList)) {
            //全部删除
            result.put("removeList", oldList.stream().distinct().collect(Collectors.toList()));
            return result;
        } else if (isEmpty(oldList)) {
            //全部新增
            result.put("addList", newList.stream().distinct().collect(Collectors.toList()));
            return result;
        } else if (!isEmpty(newList) && !isEmpty(oldList)) {
            //先循环newList，用oldList是否包含newList的元素来获取新增的数据
            result.put("addList", newList.stream().filter(s -> !oldList.contains(s)).distinct().collect(Collectors.toList()));
            //再循环oldList，用newList是否包含oldList的元素来获取删除的数据
            result.put("removeList", oldList.stream().filter(s -> !newList.contains(s)).distinct().collect(Collectors.toList()));
        }
        return result;
    }
}
